package com.myexam.action;

import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.myexam.model.Exam;
import com.myexam.model.Subject;
import com.myexam.model.Teacher;
import com.myexam.service.SubjectService;
import com.myexam.service.TeacherService;
import com.opensymphony.xwork2.ActionSupport;
@Controller
@Scope("prototype")
public class AddExamAction extends ActionSupport implements SessionAware{

	
	private static final long serialVersionUID = -1190752131773552586L;
	@Autowired
	private TeacherService teacherService;
	@Autowired
	private SubjectService subjectService;
	private Map<String, Object> session;
	private String examName;
	private String examBegin;
	private String examTime;
	private String examScore;
	private int examSubjectNum;
	private String examStage;
	public String getExamName() {
		return examName;
	}
	public void setExamName(String examName) {
		this.examName = examName;
	}

	public String getExamBegin() {
		return examBegin;
	}
	public void setExamBegin(String examBegin) {
		this.examBegin = examBegin;
	}
	public String getExamTime() {
		return examTime;
	}
	public void setExamTime(String examTime) {
		this.examTime = examTime;
	}
	public String getExamScore() {
		return examScore;
	}
	public void setExamScore(String examScore) {
		this.examScore = examScore;
	}
	public Map<String, Object> getSession() {
		return session;
	}
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	
	public int getExamSubjectNum() {
		return examSubjectNum;
	}
	public void setExamSubjectNum(int examSubjectNum) {
		this.examSubjectNum = examSubjectNum;
	}
	public String getExamStage() {
		return examStage;
	}
	public void setExamStage(String examStage) {
		this.examStage = examStage;
	}
	public String execute()throws Exception{
		Teacher teacher = (Teacher) session.get("teacherInfo");
		Exam exam = new Exam();
		exam.setExam_name(examName);
		exam.setExam_begin(examBegin);
		exam.setExam_score(examScore);
		exam.setExam_use(examTime);
		exam.setExam_stage(Integer.parseInt(examStage));
		List<Subject> list = subjectService.randomFindSubject(examSubjectNum);
		String examSubject = "";
		for (int i = 0; i < list.size(); i++) {
			examSubject = examSubject+","+list.get(i).getSubjectID();
		}
		exam.setExam_subject(examSubject);
		teacherService.saveExam(exam, teacher);
		return SUCCESS;
	}
}
